// we7/pages/mine/minePSIntegralExchange.js
var util = require("../../resource/utils/util.js");
const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    clock: '',
    StatusBar: app.globalData.StatusBar, //模态动画
    CustomBar: app.globalData.CustomBar,
    order_id:"",
    goods_id:"",
    goods_name:"",
    goods_num:"",
    pay_price:"",
    specification_name:"",
    receiving_goods_type:"",
    shop_name:"",
    name:"",
    mobile:"",
    province:"",
    city:"",
    district:"",
    address:"",
    goods_original_price:"",
    order_no:"",
    pay_type:"",
    create_time:"",
    order_remark:'',
    goods_image:"",
    order_state:"",
    self_taking_code:"",
    self_taking_name:"",
    self_taking_phone:"",
    self_taking_time:"",
    is_cancel:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // count_down(this);
    // console.log(options)
    console.log(options)
    var that=this
    const value = wx.getStorageSync('openid');
    console.log(value)
    app.util.request({
      url: 'admin/WxGoodsApi/orderInfo',
      method: 'post',
      data:{
        order_id:options.order_id,
        uid:value
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          var data = res.data.data
          // console.log(data)
            data['goods_image'] = app.globalData.url + data['goods_image'];
            // 转换时间戳
          var time = data.create_time
          console.log(util)
          data["create_time"] = util.formatTimeTwo(time, 'Y-M-D h:m:s')
          // var sjc = 1550714678;
          // console.log(formatTimeTwo(time, 'Y-M-D h:m:s'));
// 时间戳转化的日期格式： 2019-02-21 10:04:38
          that.setData({
            order_id:data.id,
            goods_id: data.goods_id,
            goods_name: data.goods_name,
            goods_num: data.goods_num,
            pay_price: data.pay_price,
            specification_name: data.specification_name,
            receiving_goods_type: data.receiving_goods_type,
            shop_name: data.shop_name,
            name: data.name,
            mobile: data.mobile,
            province: data.province,
            city: data.city,
            district: data.district,
            address: data.address,
            goods_original_price: data.goods_original_price,
            order_no: data.order_no,
            pay_type: data.pay_type,
            create_time: data.create_time,
            order_remark: data.order_remark,
            goods_image: data.goods_image,
            order_state: data.order_state,
            self_taking_code: data.self_taking_code,
            self_taking_name: data.self_taking_name,
            self_taking_phone: data.self_taking_phone,
            self_taking_time: data.self_taking_time,
            is_cancel: data.is_cancel

          })
        }

      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  //模态动画
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 取消订单
  quOrder(e){
    var that = this
    const value = wx.getStorageSync('openid');

    app.util.request({
      url: 'admin/WxGoodsApi/confirmOrCancelOrder',
      data: {
        order_id: that.data.order_id,
        uid: value,
        type: 2
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          setTimeout(function () {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }, 200)
          wx.redirectTo({
            url: './mine'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 立即付款
  zhiOrder(e){
    var that = this
    const value = wx.getStorageSync('openid');

    app.util.request({
      url: 'admin/WxGoodsApi/confirmPay',
      data: {
        order_no: that.data.order_no,
        uid: value
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          // 拉起支付
          app.startpay(res.data.data)
          // setTimeout(function () {
          //   wx.showToast({
          //     title: res.data.msg,
          //     icon: 'none',
          //     duration: 2000
          //   })
          // }, 200)
        }
      },
    })
  },
  // 确认收货
  queOrder(e){
    var that = this
    const value = wx.getStorageSync('openid');
    app.util.request({
      url: 'admin/WxGoodsApi/confirmOrCancelOrder',
      data: {
        order_id: that.data.order_id,
        uid: value,
        type: 1
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          that.hideModal(e)
          setTimeout(function () {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }, 200)
          wx.redirectTo({
            url: './mine'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '服务器响应失败',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  // 去评价
  quEvaluate(){
    var that=this
    var goods_id = that.data.goods_id
    var order_id = that.data.order_id
    wx.navigateTo({
      url: './mineOrderEvaluate?goods_id='+goods_id+'&'+'order_id='+order_id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // wx.reLaunch({
    //   url: '../index/index'
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})